package es.pirata.extra; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.apache.http.util.ByteArrayBuffer; import android.util.Log; import android.widget.ProgressBar; public class CtrlNet { private static String webUrlProxy = "http://m.pirata.cat/request/proxyJsonMobile.php"; private static CtrlNet INSTANCE = null; public static CtrlNet getInstance() { if (INSTANCE == null) { INSTANCE = new CtrlNet(); } return INSTANCE; } public void update(ProgressBar pb) throws Exception { if (CtrlDb.getInstance().startUpdate()) { String url, body; url = webUrlProxy+"?rss&lang=es"; body = downloadBody(url); pb.setProgress(50); CtrlFile.getInstance().saveFile("rss", body); pb.setProgress(100); CtrlDb.getInstance().endUpdate(); } } // ----- PRIVATE private String downloadBody(String urlString) { Log.d("downloadBody", urlString); try { URL myURL = new URL(urlString); URLConnection ucon = myURL.openConnection(); ucon.setConnectTimeout(10000); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is, 65535); ByteArrayBuffer baf = new ByteArrayBuffer(65535); int current = 0; while((current = bis.read()) != -1) { baf.append((byte)current); } return new String(baf.toByteArray()); } catch (IOException e) { e.printStackTrace(); return new String(); } } }